<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="隐藏域名称（ID）"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="隐藏域值（ID）"%>

<%@ attribute name="parentName" type="java.lang.String" required="false" description="所有父节点（ID）"%>
<%@ attribute name="parentNameValue" type="java.lang.String" required="false" description="所有父节点的ID值"%>

<%@ attribute name="labelName" type="java.lang.String" required="true" description="输入框名称（Name）"%>
<%@ attribute name="labelValue" type="java.lang.String" required="true" description="输入框值（Name）"%>
<%@ attribute name="title" type="java.lang.String" required="true" description="选择框标题"%>
<%@ attribute name="url" type="java.lang.String" required="true" description="树结构数据地址"%>
<%@ attribute name="fullTree" type="java.lang.Boolean" required="false" description="是否保存所有父节点的名称"%>
<%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否显示复选框，如果不需要返回父节点，请设置notAllowSelectParent为true"%>
<%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的编号（不能选择的编号）"%>
<%@ attribute name="isAll" type="java.lang.Boolean" required="false" description="是否列出全部数据，设置true则不进行数据权限过滤（目前仅对Office有效）"%>
<%@ attribute name="notAllowSelectRoot" type="java.lang.Boolean" required="false" description="不允许选择根节点"%>
<%@ attribute name="notAllowSelectParent" type="java.lang.Boolean" required="false" description="不允许选择父节点"%>
<%@ attribute name="module" type="java.lang.String" required="false" description="过滤栏目模型（只显示指定模型，仅针对CMS的Category树）"%>
<%@ attribute name="selectScopeModule" type="java.lang.Boolean" required="false" description="选择范围内的模型（控制不能选择公共模型，不能选择本栏目外的模型）（仅针对CMS的Category树）"%>
<%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允许清除"%>
<%@ attribute name="allowInput" type="java.lang.Boolean" required="false" description="文本框可填写"%>
<%@ attribute name="cssClass" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="layVerify" type="java.lang.String" required="false" description="表单验证方式"%>
<%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="smallBtn" type="java.lang.Boolean" required="false" description="缩小按钮显示"%>
<%@ attribute name="hideBtn" type="java.lang.Boolean" required="false" description="是否显示按钮"%>
<%@ attribute name="disabled" type="java.lang.String" required="false" description="是否限制选择，如果限制，设置为disabled"%>
<%@ attribute name="dataMsgRequired" type="java.lang.String" required="false" description=""%>

<input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}"/>
<input id="${id}Name" name="${labelName}" ${allowInput?'':'readonly="readonly"'} type="text" value="${labelValue}" data-msg-required="${dataMsgRequired}" class="${cssClass}" lay-verify="${layVerify}" style="${cssStyle}"  placeholder="请选择..."/>
<input id="${id}parentName" name="${parentName}" type="hidden" value="${parentNameValue}"/>
<script type="text/javascript">
	layui.use(['form'], function() {
		var $ = layui.jquery,form = layui.form(),layer = parent.layer === undefined ? layui.layer : parent.layer;

		function getParentsName(nodes,name){

			if(nodes.parentTId != null){
				name = nodes.getParentNode().name+"->"+name;
				return getParentsName(nodes.getParentNode(),name);
			}else{
				return name;
			}

		}

		$("#${id}Button, #${id}Name").click(function(){
		//$('body').off("click").on('click', "#${id}Button, #${id}Name",function(){

			// 是否限制选择，如果限制，设置为disabled
			if ($("#${id}Button").hasClass("disabled")){
				return true;
			}
			parent.layer.load(2);
			// 正常打开
			parent.layer.open({
				type: 2,
				title: '选择${title}',
				shadeClose: false,
				shade: 0.8,
				area: ['300px', '55%'],
				maxmin: true,
				content: '<%=request.getContextPath()%>/common/treeSelect?url=${url}&module=${module}&checked=${checked}&extId=${extId}&isAll=${isAll}',
				btn: ['确定','清除','取消'],
				yes: function(index,layero) {
					var tree = layero.find("iframe")[0].contentWindow.tree;
					var ids = [], names = [], nodes = [] ,parentIds=[],parentNames=[], prices=[],delayMonths=[],serviceMoneys=[],businessTypes=[],unitQuantitys=[],unitTypes=[] ;
					if ("${checked}" == "true"){
						nodes = tree.getCheckedNodes(true);
					}else{
						nodes = tree.getSelectedNodes();
					}

					for(var i=0; i<nodes.length; i++) {
						ids.push(nodes[i].id);
						names.push(nodes[i].name);

						parentNames.push(getParentsName(nodes[i],nodes[i].name));
						if(nodes[i].pIds){
							parentIds.push(nodes[i].pIds);
						}

						//modify by 包健敏  如果是选择产品时，后台输出价格.
						if(nodes[i].price){
							prices.push(nodes[i].price);
						}
						if(nodes[i].delayMonth){
							delayMonths.push(nodes[i].delayMonth);
						}
						if(nodes[i].serviceMoney){
							serviceMoneys.push(nodes[i].serviceMoney);
						}
						if(nodes[i].businessType){
							businessTypes.push(nodes[i].businessType)
						}
                        if(nodes[i].unitQuantity){
                            unitQuantitys.push(nodes[i].unitQuantity)
                        }
                        if(nodes[i].unitType){
                            unitTypes.push(nodes[i].unitType)
                        }

					}


					$("#${id}Id").val(ids.join(",").replace(/u_/ig,""));
					$("#${id}Name").val(parentNames.join(","));

					if ($(".modal-body #areaIdButton").size()>0) {
						if ($("#areaIdButton").parents(".modal-body").find(".tabbable").size() > 0) {
							var tabobj = $("#areaIdButton").parents(".modal-body").find(".tabbable .nav-tabs");
							var addedarea = tabobj.find("a.btn").eq(0).clone();
							addedarea.attr("class", "btn btn-primary btn_notset");
							addedarea.attr("prevclass", "btn btn-primary btn_notset");
							addedarea.attr("prev_class2", "btn btn-primary btn_notset");

							var areaid_str = ids.join(",").replace(/u_/ig, "");
							addedarea.attr("id", areaid_str);
							addedarea.attr("area_id", areaid_str);
							addedarea.attr("parent_area_id", parentIds);
							addedarea.find("i").hide()
							addedarea.find("i").attr("prev_display", "none");
							addedarea.find("span").html(parentNames.join(","));
							tabobj.append(addedarea);

							bindAreabtnClickEvent();

						}
					}

					if(parentIds){
						$('#${id}parentName').val(parentIds);
					}

					//用于服务工单计算金额
					if($("#serviceMoney")){
                        var type = unitTypes.join(",");
                        var money = serviceMoneys.join(",");
                        var unitQuantity = unitQuantitys.join(",");
						if(money){
							if(type=="1"){ //如果是 1  则表示 按小时计算费用
								var begin = new Date($("#serviceBegin").val()).getTime();
								var end = new Date($("#serviceEnd").val()).getTime();
								var total = (end-begin)/1000;
								//var day = parseInt(total/(24*60*60));//计算整数天数
								//var afterDay =total-day*24*60*60;//取得算出天数后剩余的秒数
								//var hour = parseInt(afterDay/(60*60));//计算整数小时数

                                var time = parseInt(total/(parseFloat(unitQuantity)*60*60));

                                money = parseInt(money)*time;
							}
							$("#serviceMoney").val(money);
						}
					}

                    if(!parentIds.join(",")) {
                        if(parent){
                            if(parent.initWaiters){
                                parent.initWaiters(ids.join(","));
                            }
                            if(parent.initSupplierSelect){
                                parent.initSupplierSelect(ids.join(","));
                            }
                        }
                    }

					//modify by 包健敏  如果是选择产品时，回写产品价格和每月平摊.
					if($("#chargeMoney")){
						$("#chargeMoney").val(prices.join(","))
					}
					if($("#productPrice")){
						$("#productPrice").val(prices.join(","))
					}

					if($("#delayMonth")){
						$("#delayMonth").val(delayMonths.join(","));
						var preDeadLine = '${lastEndDate}';
						if(preDeadLine){
							$.ajax({
								type : 'POST',
								url : '<%=request.getContextPath()%>/charge/getServiceEndDate',
								data : {
									'preDeadline':preDeadLine,
									'delayMonth':$('#delayMonth').val()
								},
								success : function(data) {

									data = data.replace("\"","").replace("\"","");
									if(data){
										$("#serviceTo").val(data);
									}
								}
							});
						}
					}
					if($("#perMonth")){
						if(prices.join(",")&&delayMonths.join(",")){
							$("#perMonth").val(parseFloat(prices.join(","))/parseFloat(delayMonths.join(",")));
						}
					}
					parent.layer.close(index);
					parent.layer.closeAll("loading");
				},
				cancel: function(){
					parent.layer.closeAll("loading");
				},
				btn2:function(){
					$("#${id}Id").val("");
					$("#${id}Name").val("");
					parent.layer.closeAll("loading");
				},
				btn3:function(){
					parent.layer.closeAll("loading");
				}
			});

		});
	});





</script>